const http = require('http')

async function testAPI () {
  try {
    console.log('测试小说列表API...')

    const options = {
      hostname: 'localhost',
      port: 3000,
      path: '/api/novels',
      method: 'GET',
      headers: {
        'Content-Type': 'application/json'
      }
    }

    const req = http.request(options, res => {
      let data = ''

      res.on('data', chunk => {
        data += chunk
      })

      res.on('end', () => {
        console.log('状态码:', res.statusCode)
        console.log('响应数据:')
        try {
          const jsonData = JSON.parse(data)
          console.log(JSON.stringify(jsonData, null, 2))

          if (jsonData.success && jsonData.data) {
            console.log(`\n✅ 成功获取 ${jsonData.data.length} 本小说`)
            jsonData.data.slice(0, 3).forEach(novel => {
              console.log(
                `- ${novel.title} (${novel.author}) - ${novel.status}`
              )
            })
          }
        } catch (e) {
          console.log('原始响应:', data)
        }
        process.exit(0)
      })
    })

    req.on('error', e => {
      console.error('请求错误:', e.message)
      process.exit(1)
    })

    req.setTimeout(5000, () => {
      console.error('请求超时')
      req.destroy()
      process.exit(1)
    })

    req.end()
  } catch (error) {
    console.error('测试失败:', error)
    process.exit(1)
  }
}

testAPI()
